var express = require('express');
var router = express.Router();
var captchapng = require('captchapng');
var mongo =require('../moudule/mongo');
var db = mongo.stu;
var sms_db = mongo.sms;
var sessioncode ;
var zy = require('../moudule/ypsms');
var MessageXSend = require('../sms/messageXSend.js')
var submail = new MessageXSend();

/* GET users listing. */

router.get('/getCode',function(req, res, next){
    res.setHeader('Access-Control-Allow-Origin','*');
    res.setHeader('Access-Control-Allow-Methods','put,post,get,delete,option');
    var phone = req.param("phone");
    var num ="";
    var count = 1;
    for(var i=0;i<6;i++){
        num+= Math.floor(Math.random()*10);
    }
    console.log(num);
    submail.add_to(phone);
    submail.add_var("code", num);
    submail.add_var("time", "1分钟");
    submail.set_project('6L8VW4');
    if(phone){
     var condiction = {phone: phone};
     var query = {$set: {death: false, code: num}};
     var newQuery = {$set: {death: true}};
     sms_db.find(condiction,function(err,result){
        if(err) console.log(err+"err");
        if(result && result.length!=0){
          console.log(result[0].death);
          if(result[0].death == false){
            ++count;
            if(count == 3) {
              res.json({
                "status":"error",
                "msg":"发送次数过多,2小时后再尝试"
              });
              setTimeout(function() {
                count =1;
                }, 2*3600*1000)
            } else {
                submail.xsend(function(result){
                  result = JSON.parse(result);
                  console.log(result.status);
                  if(result.status == "success"){
                   res.json({
                    "code":num,
                    "status":"success",
                    "msg":"发送成功"
                  });
                   sms_db.update(condiction,query,function(err,result){
                    console.log(err);
                    console.log(result);
                  });
                 } else {
                   res.json({
                    "code":num,
                    "status":"error",
                    "msg":"发送失败:"+result.msg
                  }) 
             }
        });
                setTimeout(function(){
                sms_db.update(condiction,newQuery,function(err,result){
                console.log(err);
                console.log(result);
              });
              },60000); 
              }
          } else {
            submail.xsend(function(result){
              result = JSON.parse(result);
              console.log(result.status);
              if(result.status == "success"){
                res.json({
                "code":num,
                "status":"success",
                "msg":"发送成功"
              });
               sms_db.update(condiction,query,function(err,result){
                console.log(err);
                console.log(result);
              });
             } else {
               res.json({
                "code":num,
                "status":"error",
                "msg":"发送失败:"+result.msg
              }) 
             }
        });
            setTimeout(function(){
                sms_db.update(condiction,newQuery,function(err,result){
                console.log(err);
                console.log(result);
              });
              },60000); 
          }
        } else {
          submail.xsend(function(result){
            result = JSON.parse(result);
            console.log(result.status);
            if(result.status == "success"){
               sms_db.create({phone:phone,code:num,death:false},function(err,result){
                console.log(err);
                console.log(result);
              });
               res.json({
                "code":num,
                "status":"success",
                "msg":"发送成功"
             })
            } else {
               res.json({
                "code":num,
                "status":"error",
                "msg":"发送失败:"+result.msg
            }) 
          }
        });
          setTimeout(function(){
                sms_db.update(condiction,newQuery,function(err,result){
                console.log(err);
                console.log(result);
              });
          },60000); 
        }
    })
    } else {
        res.json({
            "status":"error",
             "msg":"缺少必传参数"
        }) 
    }
    
});

router.get('/checkCode',function(req,res,next) {
    res.setHeader('Access-Control-Allow-Origin','*');
    res.setHeader('Access-Control-Allow-Methods','post,get');
    var param = {
      phone:req.param('phone'),
      code:req.param('code')
    };
    console.log(param);
    console.log(sms_db);
    sms_db.find(param,function(err,result){
      console.log(err);
      console.log(result);
      if(result && result.length!=0){
        console.log(result[0].death);
        if(result[0].death == true){
          res.json({msg:"验证码已经过期",status:"error"})
        } else {
          res.json({msg:"验证码验证通过",status:"success"})
        }
      } else {
        res.json({msg:"验证码不正确",status:"success"})
      }
    })
})

module.exports = router;
